package com.api.nanc.action;

import com.api.nanc.util.BaseUtil;
import com.api.shihua.util.LogTool;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocViewer;
import weaver.general.Util;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.action.BaseAction;
/*
      将回复流程的附件以及 回复内容更新到 主表明细字段 (需要对文档的权限进行赋权)

 */
public class Submitfile extends BaseAction {
    private final static LogTool loghr = new LogTool("/log/hr/",false);
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid=requestInfo.getRequestid();
        String workflowid=requestInfo.getWorkflowid();
        loghr.writeLog("Submitfile  requestid"+requestid+" "+workflowid);
        String hffj="";//回复附件
        String bz="";//回复备注
        String mainrequestid="";//主流程ID
        String detailid="";//明细ID
        MainTableInfo mainTableInfo=requestInfo.getMainTableInfo();
        Property[] Property= mainTableInfo.getProperty();
        for(Property prop:Property){
            String name=prop.getName();
            String value=prop.getValue();
            if("hffj".equalsIgnoreCase(name)){
                hffj= Util.null2String(value).trim();
            }
            if("bz".equalsIgnoreCase(name)){
                bz= Util.null2String(value);
            }
            if("mainrequestid".equalsIgnoreCase(name)){
                mainrequestid= Util.null2String(value).trim();
            }
            if("detailid".equals(name)){
                detailid= Util.null2String(value).trim();
            }
        }
        loghr.writeLog("Submitfile  hffj"+hffj+" "+bz);
        //文档赋权
        //获取流程的当前 节点的操作者
        if(!"".equals(mainrequestid)){
            RecordSet rs2 = new RecordSet();
            String hrmids=Util.null2String(BaseUtil.getNodeOpterV1(mainrequestid)).trim();
            if(!"".equals(hrmids)&&!"".equals(hffj)){//赋权文档权限

                String[] str=hrmids.split(",");
                for(String demo:str){
                    demo=Util.null2String(demo).trim();
                    if(!"".equals(demo)){
                      String[] hffjs=  hffj.split(",");
                      for(String demo1:hffjs){
                          demo1=Util.null2String(demo1).trim();
                          if(!"".equals(demo1)){
                              //插入赋权数据
                              DocViewer dv = new DocViewer();

                              rs2.executeUpdate("insert into DocShare(docid,sharetype,seclevel,rolelevel,sharelevel,"
                                              +"userid,subcompanyid,departmentid,roleid,foralluser,crmid,orgGroupId,downloadlevel,"
                                              +"seclevelmax,includesub,orgid,allmanagers)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                                      ,Util.null2String(demo1).trim(),1,0,0,1,Util.null2String(demo).trim(),0,0,0,0,0,0,1,255,0,0,0);
                              try {
                                  dv.setDocShareByDoc(demo1);
                              } catch (Exception e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
                          }
                      }
                    }
                }
            }
            //将附件以及 备注更新到明细行
            String formid=BaseUtil.getRequestInfoById(mainrequestid).getString("formid");
            loghr.writeLog("update formtable_main_"+formid+"_dt1 set fj='"+hffj+"',bz='"+bz+"',zt='2' where id="+detailid);
            rs2.execute("update formtable_main_"+formid+"_dt1 set fj='"+hffj+"',bz='"+bz+"',zt='2' where id="+detailid);
        }

        return super.execute(requestInfo);
    }
}
